# -*- coding: UTF-8 -*-
# Project : huobiao
# File : cmef.py
# IDE : PyCharm
# Author : 博科（鑫贝西）田聪
# Date : 2021/11/6 14:04
import time

import requests
from faker import Factory
from lxml import etree
from tools.tomysql import MYSQL

class CMEF:
    def __init__(self):
        self.page_size = 30 #每页的信息数


    def req_post(self,page):
        url = 'https://www.cmef.com.cn/exhibitorlist/ExhibitorQuery'
        parmas = {"exhibitionIds": ["e9defafb-5d6b-42d2-b523-c2abad413c75", "d2f77a00-c615-4cb6-85b4-3058f515c0ab",
                                    "af920968-535d-4b73-aa41-f1d092077e34"], "categories": [], "text": "",
                  "pageSize": "30",
                  "pageIndex": f"{page}", "exhibitionMap": [{"Id": "e9defafb-5d6b-42d2-b523-c2abad413c75", "IsShow": 1},
                                                      {"Id": "d2f77a00-c615-4cb6-85b4-3058f515c0ab", "IsShow": 1},
                                                      {"Id": "af920968-535d-4b73-aa41-f1d092077e34", "IsShow": 0},
                                                      {"Id": "5c096d71-c5b8-4432-a0dc-7f3175b25596", "IsShow": 1},
                                                      {"Id": "601156ff-abf5-439f-83df-8e5f9ef180f2", "IsShow": 1},
                                                      {"Id": "a20542d0-3f06-4163-bc4b-26f6cac7d4b3", "IsShow": 0},
                                                      {"Id": "667ed22f-b921-4910-87b1-6ed9575ce6b1", "IsShow": 1},
                                                      {"Id": "f7dc8fe5-c2ce-41ba-93df-80b1c632dac4", "IsShow": 1},
                                                      {"Id": "678f8c16-9474-4762-b012-9d5dad231e5d", "IsShow": 0}]}
        headers = {
            'Host':'www.cmef.com.cn',
            'User-Agent':Factory.create().user_agent(),
            'Accept':'*/*',
            'Accept-Language':'zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2',
            'Accept-Encoding':'gzip, deflate',
            'Content-Type':'application/json',
            'X-Requested-With':'XMLHttpRequest',
            'Connection':'keep-alive',
            'Pragma':'no-cache',
            'Cache-Control':'no-cache',
                    }
        response = requests.post(
            url = url,
            headers = headers,
            params= parmas
        )
        if response.status_code == 200:
            return response

    def start_request(self,page=0):
        ennames = []
        # page = 0
        while True:
            print(f'####### 当前页码 - -{page}- - ######')
            response = self.req_post(page)
            _HTML = etree.HTML(response.text)
            divs = _HTML.xpath('//div[@class="exc-item-top clearfix"]')
            for div in divs:
                enname = div.xpath('.//div[@class="exc-item-title inner"]/text()')[0].replace(r'\n','')
                ennames.append(enname)
            if len(divs)<self.page_size:
               break
            else:
                page += 1
                time.sleep(20)

        return ennames



if __name__ == '__main__':
    c = CMEF()
    from tqdm import tqdm
    ens = c.start_request()
    sqls = MYSQL()
    for en in tqdm(ens):
        # print(en)
        sql_text = "INSERT INTO `en_information`(`entName`) VALUES ('{}')".format(en)
        sqls.sql_write(sql_text, en)
